import React, {Component} from 'react'
import { Link } from 'react-router'
import Writebar from './writebar'
import './sm-extend.min.css'
import Getemailbar from './getemailbar'
import Userlistbar from './userlistbar'
import './sm.min.css'
import './index.css'
import $ from 'jquery'

class Userlist extends Component{
    constructor(){
        super();
        this.state={
            list:'',
            userinfo:''
        }
    }
    render(){
        var data=this.state.userinfo
        var rows=[];
        for(var i=0;i<data.length;i++){
            rows.push(<div className="userlistdiv col-100">
                <div className="usersInfo col-100">用户名:{data[i]}</div>
            </div>)
        }
        return(
            <div>
            <div className=" inboxlist content">
            <div className="row no-gutter">
            {rows}
                <div className="col-100">
                    <div className="col-20 addbtndiv">
                        <button className="button button-mid button-fill button-success addbtn">增加</button>
                    </div>
                    <div className="col-80 addinputdiv">
                        <input className="adduser" placeholder="请输入增加的用户名" />
                        <button className="tijiao button button-mid button-fill button-success">提交</button>
                    </div>

                </div>
            </div>
            </div>


            <Userlistbar/>
            </div>
    )

    }
    componentDidMount=()=>{
        var username = localStorage.getItem('name')
        var data={
            username:username,
        }
        $.post('http://127.0.0.1:8080/inboxtheme',data,function (data,textStatus,xhr) {
        this.setState({
            list: data,
            userinfo: data[0].userlist

        });
            $('.userlistdiv').click(function(){
                var sy = $(this).index();
                var username = localStorage.getItem('name')
                var data={
                    index:sy,
                    username:username,
                }
                console.log(data)
                $.post('http://127.0.0.1:8080/forname',data,function (data,textStatus,xhr) {
                    console.log(data)
                    localStorage.setItem('forname',data)
                    window.location.href="http://127.0.0.1:3000/#/content";
                })
            })
        }.bind(this))

        $('.addbtn').click(function(){
            $('.addinputdiv').css('display','block')
        })
        $('.tijiao').click(function(){
            var username = $('.adduser').val();
            var localname = localStorage.getItem('name')
            var data={
                username:username,
                localname:localname,
            }

            $.post('http://127.0.0.1:8080/adduser',data,function (data,textStatus,xhr) {
                if(String(data)=="equal"){
                    alert('只能增加非本人用户名');
                }else if(String(data)=="error"){
                    alert('通讯录已有此人');
                }else if(String(data)=="kong"){
                    alert('查无此人');
                }else{
                    alert('添加成功');
                    window.location.reload();
                }


            }.bind(this))

});




}}



export default Userlist